package com.zdth.qywxpush.boot.util;

import com.zdth.qywxpush.boot.model.QyImage;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

/**
 * @Author Z-7
 * @Date 2022/9/21
 */
public class QyBootUtil {
    public static QyImage getImageMd5AndBase64(File file) {
        String base64 = "";
        String md5 = "";
        try {
            FileInputStream inputStream = new FileInputStream(file);
            byte[] bs = new byte[inputStream.available()];
            inputStream.read(bs);
            base64 = Base64.getEncoder().encodeToString(bs);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 获取md5值
        try {
            FileInputStream inputStream = new FileInputStream(file);
            byte[] buf = new byte[inputStream.available()];
            inputStream.read(buf);
            md5 = DigestUtils.md5Hex(buf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new QyImage(base64,md5);
    }
}
